1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class RotateCamera : MonoBehaviour, IInputReceiver {
6
7     
[SerializeField]
8     
private Transform target;
9     
[SerializeField]
10     
private float horizAngleMove;
11     
[SerializeField]
12     
private float vertAngleMove;
13
14     
private float lookAngleY;
15     
public float turnSmoothing;
16
17     
public float xAngleMax = 75f;
18     
public float xAngleMin = 45f;
19     
public float lookAngleX;
20
21     
public Transform pivotTransform;
22     
private Vector3 pivotEulers;
23
24     
private InputManager inputManager;
25     
private Quaternion newRotY;
26     
private Quaternion newRotX;
27
28     
private bool rotate = false;
29
30     
void Start() {
31         pivotEulers = pivotTransform.rotation.eulerAngles;
32         EnableInput();
33         inputManager = InputManager.Instance;
34     }
35
36     
public void EnableInput() {
37         InputManager.InputEvent += OnInputEvent;
38     }
39
40     
public void DisableInput() {
41         InputManager.InputEvent -= OnInputEvent;
42     }
43
44     
void OnDisable() {
45         DisableInput();
46     }
47
48     
protected void Update() {
49         
if (!rotate) return;
50         lookAngleY += inputManager.MouseAxis.x * horizAngleMove;
51         newRotY = Quaternion.Euler(
0f,lookAngleY,0f);
52
53         lookAngleX += inputManager.MouseAxis.y * vertAngleMove;
54         lookAngleX = Mathf.Clamp(lookAngleX, -xAngleMin, xAngleMax);
55         newRotX = Quaternion.Euler(lookAngleX, pivotEulers.y, pivotEulers.z);
56
57         
if (turnSmoothing > 0) {
58             pivotTransform.localRotation = Quaternion.Slerp(pivotTransform.localRotation, newRotX, turnSmoothing * Time.deltaTime);
59             transform.localRotation = Quaternion.Slerp(transform.localRotation, newRotY, turnSmoothing * Time.deltaTime);
60         }
else {
61             transform.localRotation = newRotY;
62             pivotTransform.localRotation = newRotX;
63         }
64     }
65
66     
public void OnInputEvent(InputActionType action) {
67         
switch (action) {
68             
case InputActionType.ROTATE:
69             rotate =
true;
70             
break;
71
72             
case InputActionType.STOP_ROTATE:
73             rotate =
false;
74             
75             
break;
76         }
77     }
78
79 }


Gõ tìm kiếm nhanh...